//
//  GengTieView.m
//  GuShiNews
//
//  Created by huachun on 2019/8/23.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "GengTieView.h"
#import "HiAboutTextView.h"
#import "LunTanModel.h"
#import "AddLunCommentViewModel.h"

@interface GengTieView ()<UITextViewDelegate,AddLunCommentDelegate>{
    CGRect     hereframe;
}

@property (nonatomic, strong) HiAboutTextView *textView;

@property (strong, nonatomic) UIButton       *Okbutton;
@property (strong, nonatomic) UIButton       *Canelbutton;

@end

@implementation GengTieView

+(GengTieView *)Show:(CGRect)rect{
    GengTieView *showview     = [[GengTieView alloc] initWithFrame:rect];
    showview.backgroundColor     = [UIColor whiteColor];
    showview.layer.cornerRadius  = 4.0;
    showview.layer.masksToBounds = YES;
    showview.layer.shadowColor   = [JonesClass stringTOColor:@"#333333"].CGColor;
    showview.layer.shadowOffset  = CGSizeMake(0,4);
    showview.layer.shadowOpacity = 0.9;
    showview.bg_button                 = [[UIButton alloc] init];
    showview.bg_button.frame           = CGRectMake(0, 0, RECRWITCH, RECERHEIGHT);
    showview.bg_button.backgroundColor = [UIColor blackColor];
    showview.bg_button.alpha           = 0.0;
    [showview.bg_button addTarget:showview action:@selector(show_hide:) forControlEvents:UIControlEventTouchUpInside];
    
    [[UIApplication sharedApplication].keyWindow addSubview:showview.bg_button];
    [[UIApplication sharedApplication].keyWindow addSubview:showview];
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        showview.frame           = CGRectMake(RECRWITCH/2-rect.size.width/2, RECERHEIGHT/2-rect.size.height/2-100, rect.size.width, rect.size.height);
        showview.bg_button.alpha = 0.3;
    } completion:^(BOOL finished) {
        
    }];
    
    return showview;
}

-(void)textViewDidChange:(UITextView *)textView{

}

-(void)show_hide:(GengTieView *)sender{
    [_textView.textview resignFirstResponder];
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        super.frame          = CGRectMake(RECRWITCH/2-self->hereframe.size.width/2, RECERHEIGHT, self->hereframe.size.width, self->hereframe.size.height);
        self.bg_button.alpha = 0.0;
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
        [self.bg_button removeFromSuperview];
    }];
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        
        hereframe               = frame;
        
        _textView = [HiAboutTextView show:CGRectMake(10, 10, frame.size.width-20, frame.size.height-60)];
        _textView.textview.font=[UIFont boldSystemFontOfSize:18];
        _textView.textview.delegate=self;
        _textView.textview.placeholder = @"请输入跟帖内容";
        [self addSubview:_textView];
        [_textView.textview becomeFirstResponder];
        
        self.Okbutton           = [UIButton new];
        self.Okbutton.tag = 1;
        self.Okbutton.frame     = CGRectMake(frame.size.width/2+5, frame.size.height-45, (frame.size.width-40)/2, 37);
        [self.Okbutton.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [self.Okbutton addTarget:self action:@selector(showaction:) forControlEvents:UIControlEventTouchUpInside];
        self.Okbutton.backgroundColor = NavItemTitleColor;
        self.Okbutton.layer.masksToBounds=YES;
        self.Okbutton.layer.cornerRadius=5;
        [self addSubview:self.Okbutton];
        [self.Okbutton setTitle:@"发布" forState:UIControlStateNormal];
        
        self.Canelbutton           = [UIButton new];
        self.Canelbutton.tag = 0;
        self.Canelbutton.frame     = CGRectMake(15, frame.size.height-45, (frame.size.width-40)/2, 37);
        [self.Canelbutton.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [self.Canelbutton addTarget:self action:@selector(showaction:) forControlEvents:UIControlEventTouchUpInside];
        self.Canelbutton.backgroundColor = [UIColor grayColor];
        self.Canelbutton.layer.masksToBounds=YES;
        self.Canelbutton.layer.cornerRadius=5;
        [self addSubview:self.Canelbutton];
        [self.Canelbutton setTitle:@"取消" forState:UIControlStateNormal];
    }
    return self;
}

- (void)showaction:(UIButton *)sender{
    if (sender.tag==1) {
        [SVProgressHUD showWithStatus:@"上传中.."];
        AddLunCommentViewModel *viewmodel = [[AddLunCommentViewModel alloc] init:@{@"luntan_id":_Model.lun_id,@"lcomment_userid":[UserModel info].userid,@"lcomment_info":_textView.textview.text,@"to_userid":_Model.user_id}];
        viewmodel.delegate=self;
 
    }else{
        [_textView.textview resignFirstResponder];
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            super.frame          = CGRectMake(RECRWITCH/2-self->hereframe.size.width/2, RECERHEIGHT, self->hereframe.size.width, self->hereframe.size.height);
            self.bg_button.alpha = 0.0;
        } completion:^(BOOL finished) {
            [super removeFromSuperview];
            [self.bg_button removeFromSuperview];
        }];
    }
}

-(void)AddSucces:(int)value{
    if (value==1) {
        
        [SVProgressHUD showSuccessWithStatus:@"跟帖成功"];
        [_textView.textview resignFirstResponder];
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            super.frame          = CGRectMake(RECRWITCH/2-self->hereframe.size.width/2, RECERHEIGHT, self->hereframe.size.width, self->hereframe.size.height);
            self.bg_button.alpha = 0.0;
        } completion:^(BOOL finished) {
            [super removeFromSuperview];
            [self.bg_button removeFromSuperview];
        }];
        
        _Selectblock(1);
    }
}

@end
